namespace MyCompany.UserInterfaceLibrary {
	using System.Windows;
	using System.Windows.Controls;

	public class NarrowToggleButton : ToggleButtonBase {
		/// <summary>
		///     True if the border should be hidden until the user hovers over the button or selects it with the keyboard. This
		///     defaults to false.
		/// </summary>
		public static readonly DependencyProperty IsBorderlessProperty = DependencyProperty.Register("IsBorderless", typeof(bool), typeof(NarrowToggleButton), new FrameworkPropertyMetadata(false));

		static readonly Thickness thickness1 = new Thickness(1);

		static readonly Thickness thickness2 = new Thickness(2);

		/// <summary>
		///     Gets or sets the value of the <see cref="IsBorderlessProperty" /> . This is a dependency property.
		/// </summary>
		public bool IsBorderless
		{
			get
			{
				return (bool)GetValue(IsBorderlessProperty);
			}
			set
			{
				SetValue(IsBorderlessProperty, value);
			}
		}

		public override void OnApplyTemplate() {
			base.OnApplyTemplate();

			UpdateState();
		}

		protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {
			base.OnPropertyChanged(e);

			if (e.Property == IsMouseOverProperty)
				UpdateState();
		}

		protected override void UpdateState() {
			var border = (Border)GetTemplateChild("border");
			if (border != null) {
				//border.Visibility = (IsMouseOver || IsChecked == true) && IsEnabled ? Visibility.Visible : Visibility.Hidden;
				border.Visibility = !IsBorderless || IsMouseOver && IsEnabled ? Visibility.Visible : Visibility.Hidden;
				border.BorderThickness = IsMouseOver && IsEnabled ? thickness2 : thickness1;
			}
		}
	}
}
